package com.sanyue.codingsettlement.mqstudy.rabbitmq.deadLetter;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 死信交换机
 * @author: Guan
 **/
public class DeadLetter {
    public static void main(String[] args) throws Exception{
        //创建连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        //声明死信交换机
        channel.exchangeDeclare("dead_letter_exchange","direct");


        //指定死信队列配置
        Map<String, Object> arg = new HashMap<String, Object>();
        arg.put("x-dead-letter-exchange", "some.exchange.name");
        arg.put("x-dead-letter-routing-key", "dead");
        //声明队列，队列中的消息只能保存10s，超过10s的消息会被发送到死信交换机
        arg.put("x-message-ttl", 10000);
        channel.queueDeclare("will_dead_queue",false,false,false,arg);

        //发送消息到队列
        String message = "hello world";
        channel.basicPublish("", "will_dead_queue", null, message.getBytes("UTF-8"));
    }
}
